define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload'], function ($, undefined, Backend, Table, Form, Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'news/news/index',
                    add_url: 'news/news/add',
                    edit_url: 'news/news/edit',
                    del_url: 'news/news/del',
                    multi_url: 'news/news/multi',
                    table: 'news',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            $(".btn-edit").data("area", ["100%", "100%"]);

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'image', title: __('Image'), operate: false, formatter: Controller.api.formatter.image, events: Table.api.events.image},
                        {field: 'title', title: __('Title'), formatter: Controller.api.formatter.title},
                        {field: 'lx', title: __('Lx'), searchList: {"湛江房产":__('Lx 湛江房产'),"全国房产":__('Lx 全国房产'),"港城生活":__('Lx 港城生活'),"看房日记":__('Lx 看房日记'),"楼市快讯":__('Lx 楼市快讯'),"销售动态":__('Lx 销售动态'),"房产数据":__('Lx 房产数据'),"城市规划":__('Lx 城市规划'),"购房指南":__('Lx 购房指南'),"法律法规":__('Lx 法律法规'),"精英访谈":__('Lx 精英访谈'),"购房萌萌哒":__('Lx 购房萌萌哒'),"品牌直通车":__('Lx 品牌直通车'),"碧桂园":__('Lx 碧桂园'),"行业招聘":__('Lx 行业招聘')}, formatter: Table.api.formatter.normal},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'hot', title: __('Hot'), searchList: {"0":__('Hot 0'),"1":__('Hot 1')}, formatter: Controller.api.formatter.hot},
                        {field: 'flag', title: __('Flag'), searchList: {"0":__('Flag 0'),"1":__('Flag 1')}, formatter: Controller.api.formatter.flag},
                        {field: 'attribute', title: __('Attribute'), searchList: {"0":__('Attribute 0'),"1":__('Attribute 1'),"2":__('Attribute 2')}, visible: false},
                        {field: 'admin.nickname', title: __('Admin.nickname')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showColumns: false,
                showToggle: false,
                showExport: false
            });

            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            // 标签selectPage下拉选择器
            $("#c-tag_ins").data("params", function (obj) {
                var category = $('#c-category_id').val()
                // console.log(category)
                if(category == 0){
                    // console.log('空值')
                    // 没有数据flag=2 所以获取返回的是空 显示无结果
                    return {custom: {flag:2}}
                }
                // return {custom: {type: $("#c-type").val()}};
                return {custom: {flag:1},custom: {category_id: category}};
            });

            // console.log($().jquery) // 2.2.4
            $('#c-type').change(function(){
                // alert( $(this).val() );
                var type = $(this).val()
                if(type == 0){
                    $('.picture-upload').attr('style','display:none;')
                    $('.video-upload').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 1){
                    $('.picture-upload').attr('style','display:block;')
                    $('.video-upload').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 2){
                    $('.video-upload').attr('style','display:block;')
                    $('.picture-upload').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 3){
                    $('.outlink-newstype').attr('style','display:block;')
                    $('.picture-upload').attr('style','display:none;')
                    $('.video-upload').attr('style','display:none;')
                }
            })
            // 分类是 空 或者 改变值 清空下标签框
            $('#c-category_id').change(function(){
                var v = $(this).val();
                $('#c-tag_ins').siblings('ul').children('.selected_tag').remove()
                $('#c-tag_ins').val('')
            })
        },
        edit: function () {
            Controller.api.bindevent();
            // 标签selectPage下拉选择器
            $("#c-tag_ins").data("params", function (obj) {
                var category = $('#c-category_id').val()
                // console.log(category)
                if(category == 0){
                    // console.log('空值')
                    // 没有数据flag=2 所以获取返回的是空 显示无结果
                    return {custom: {flag:2}}
                }
                return {custom: {flag:1},custom: {category_id: category}};
            });

            var type = $('#c-type').val()
            // 一进页面就必须初始化一次
            // 普通模式可以不需要
            if(type == 1){
                $('.picture-upload').attr('style','display:block;')
                $('.video-upload').attr('style','display:none;')
                $('.outlink-newstype').attr('style','display:none;')
            }else if(type == 2){
                $('.video-upload').attr('style','display:block;')
                $('.picture-upload').attr('style','display:none;')
                $('.outlink-newstype').attr('style','display:none;')
            }else if(type == 3){
                $('.outlink-newstype').attr('style','display:block;')
                $('.picture-upload').attr('style','display:none;')
                $('.video-upload').attr('style','display:none;')
            }

            $('#c-type').change(function(){
                var type = $(this).val()
                if(type == 0){
                    $('.picture-upload').attr('style','display:none;')
                    $('.video-upload').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 1){
                    $('.picture-upload').attr('style','display:block;')
                    $('.video-upload').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 2){
                    $('.video-upload').attr('style','display:block;')
                    $('.picture-upload').attr('style','display:none;')
                    $('.outlink-newstype').attr('style','display:none;')
                }else if(type == 3){
                    $('.outlink-newstype').attr('style','display:block;')
                    $('.picture-upload').attr('style','display:none;')
                    $('.video-upload').attr('style','display:none;')
                }
            })

            // 分类是 空 或者 改变值 清空下标签框
            $('#c-category_id').change(function(){
                var v = $(this).val();
                $('#c-tag_ins').siblings('ul').children('.selected_tag').remove()
                $('#c-tag_ins').val('')
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                var input_id = $('#plupload-image').data("input-id") ? $('#plupload-image').data("input-id") : "";
                var preview_id = $('#plupload-image').data("preview-id") ? $('#plupload-image').data("preview-id") : "";

                if (preview_id) {
                    $(document.body).off("click", "#" + preview_id + " .btn-trash");
                    // 监听事件
                    $("#plupload-image").data("upload-success", function(data, ret) {
                        //这里进行后续操作
                        var imageCount = $('#p-image').children('li').length
                        // console.log($imageCount)
                        // var newsType = $('#c-type').val();
                        if(imageCount == 3) {
                            $(this).hide();
                        }
                        // if(newsType == 0){
                        //     if(imageCount == 3) {
                        //         $(this).hide();
                        //     }
                        // } else {
                        //     $(this).hide();
                        // }
                    });
                    // 移除按钮事件
                    $(document.body).on("click", "#" + preview_id + " .btn-trash", function () {
                        var that = $(this);
                        var path = $("#" + preview_id + " [data-url]");
                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'news/news/delCover',
                                    data: {
                                        newsid: $("#plupload-image").data('news-id'),
                                        path: path.data("url")
                                    }
                                }
                                Fast.api.ajax(options, function () {
                                    if (input_id) {
                                        $("#" + input_id).val('');
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-image").show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });
                }
                // if (input_id) {
                //     if ($("#" + input_id).val() != '') {
                //         $("#plupload-image").hide();
                //     }
                // }

                // 仿上面重新写一次图片的删除提示
                var picture_input_id = $('#plupload-picture').data("input-id") ? $('#plupload-picture').data("input-id") : "";
                var picture_preview_id = $('#plupload-picture').data("preview-id") ? $('#plupload-picture').data("preview-id") : "";

                if (picture_preview_id) {
                    $(document.body).off("click", "#" + picture_preview_id + " .btn-trash");
                    // 监听事件
                    // $("#plupload-image").data("upload-success", function(data, ret) {
                    //     //这里进行后续操作
                    //     var imageCount = $('#p-image').children('li').length
                    //     if(imageCount == 3) {
                    //         $(this).hide();
                    //     }
                    // });
                    // 移除按钮事件
                    $(document.body).on("click", "#" + picture_preview_id + " .btn-trash", function () {
                        var that = $(this);
                        var path = $("#" + picture_preview_id + " [data-url]");
                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'news/news/delCover',
                                    data: {
                                        newsid: $("#plupload-image").data('news-id'),
                                        path: path.data("url")
                                    }
                                }
                                Fast.api.ajax(options, function () {
                                    if (picture_input_id) {
                                        $("#" + picture_input_id).val('');
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-image").show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });
                }
                // 不需要隐藏按钮
                // if (picture_input_id) {
                //     if ($("#" + picture_input_id).val() != '') {
                //         $("#plupload-picture").hide();
                //     }
                // }
            },
            formatter: {
                image: function (value, row, index) {
                    value = value ? Fast.api.cdnurl(value) : '/assets/img/blank.gif';
                    var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
                    return '<a href="javascript:void(0);"><img class="' + classname + '" src="' + value + '" /></a>';
                },
                hot: function (value, row, index) {
                    return '<a class="btn-change text-success" data-url="'+$.fn.bootstrapTable.defaults.extend.multi_url+'" data-id="' + row.id + '" data-params="hot=' + (row.hot == '0' ? '1' : '0') + '"><i class="fa ' + (row.hot == '0' ? 'fa-toggle-off' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                flag: function (value, row, index) {
                    return '<a class="btn-change text-success" data-url="'+$.fn.bootstrapTable.defaults.extend.multi_url+'" data-id="' + row.id + '" data-params="flag=' + (row.flag == '0' ? '1' : '0') + '"><i class="fa ' + (row.flag == '0' ? 'fa-toggle-off' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                title: function (value, row, index) {
                    return '<a href="https://www.0759home.com/news/info/id/' + row.id + '.html" target="_blank">' + value + '</a>';
                }
            }
        }
    };
    return Controller;
});
